package com.example.jetpackcomposestate.examples

import android.os.Bundle
import android.os.Parcelable
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.example.jetpackcomposestate.ui.theme.JetpackComposeBasicTheme
import kotlinx.parcelize.Parcelize

/**
 * @author huangzehui
 * @date 5/5/2024 下午11:55 周日
 * @description
 *
 * https://www.bilibili.com/video/BV1ob4y1a7ad/?p=40&spm_id_from=pageDriver&vd_source=e07cc8cc94eb22bd76722071b03cd199
 *
 */
class StateRecoveryParcelableActivity : ComponentActivity() {

    @Parcelize
    data class City(val name: String, var county: String) : Parcelable


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            JetpackComposeBasicTheme {
                CityScreen()
            }
        }
    }

    @Composable
    private fun CityScreen() {
        // 使用 rememberSaveable 屏幕旋转后，依旧可以保留数据，这个叫做状态恢复
        val (city, setCity) = rememberSaveable {
            mutableStateOf(City(name = "TianTang", county = "ShenYu"))
        }

//        val (city, setCity) = remember {
//            mutableStateOf(City(name = "TianTang", county = "ShenYu"))
//        }

        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.padding(10.dp)
        ) {
            // 点击按钮修改 City 信息
            TextButton(onClick = {
                setCity(City(name = "Beijing", county = "China"))
            }, colors = ButtonDefaults.buttonColors()) {
                Text(text = "Click to change")
            }

            Text(text = "$city")



        }
    }

}